#include "temperature_controller.h"
#include "adc.h"
#include "motor_driver.h"
#include "motor_controller.h"
#include "function.h"

TemperatureController temperatureController;

/**
 * @brief  温度控制器初始化
 * @param  None
 * @retval None
 */
void TemperatureController_Init(void) 
{	
    
    temperatureController.setTemperature = 40;
    temperatureController.curTemperature = 0;
    temperatureController.PID.output = 0;                  
    
    temperatureController.PID.pidParam.kp=TEMPERATURE_CONTROLLER_KP;
    temperatureController.PID.pidParam.ki=TEMPERATURE_CONTROLLER_KI;
    temperatureController.PID.pidParam.kd=TEMPERATURE_CONTROLLER_KD;
    
    temperatureController.PID.outMINLimit= -3500;         // 0
    temperatureController.PID.outMAXLimit= 4500;         // PWM_DUTY_LIMIT
    
    temperatureController.PID.lastError = 0;
    
    //位置式
    temperatureController.PID.FilterPercent = 0.7;
    temperatureController.PID.iTerm = 0;
    temperatureController.PID.integrationLimit = 700;
        
}

/**
 * @brief  温度获取函数
 * @param
 * @retval 温度值
 */
float GetTemperature(void){
    
    uint16_t valueGroup[11];
    
    uint16_t* point = valueGroup;
    for(uint8_t i=0;i<11;i++,point++){
        *point = ADCConvertedValue[i];
    }
    
    bubbleSort(valueGroup,11);
    
    uint16_t value = valueGroup[5];
    uint16_t mV = value * 1000 * 33 / 10 / 4096 ;
    uint32_t Rx100 = mV * 1000 * 100 / ( 26500 - mV );
    return (2.65612f * Rx100/100.0f - 265.9672f );
}

/**
 * @brief  温度设置函数
 * @param  nSpeed 设定的温度值,单位:摄氏度
 * @retval None
 */
void SetTemperature(float nTemperature){ 
    temperatureController.setTemperature = nTemperature;
    
    if(nTemperature<=75)
        temperatureController.PID.integrationLimit=750;
    else if(nTemperature>75 && nTemperature<=125)
        temperatureController.PID.integrationLimit=50*nTemperature-3000;
    else if(nTemperature>125)
        temperatureController.PID.integrationLimit=3250;
        
}


/**
 * @brief  温度周期控制任务
 * @param  任务控制码
 * @retval 任务状态
 */
uint8_t TemperatureControlTask(uint8_t info){

    if(info==0)
        return 0;
    
    temperatureController.curTemperature = GetTemperature();

    POSPID_Update(&temperatureController.PID,temperatureController.setTemperature,temperatureController.curTemperature,0.05);

    //分程控制
    if(temperatureController.PID.output >-500 ){
        TIM1->CCR1 = (temperatureController.PID.output + 500)*2;
        SetMotorSpeed(0,0);
    }else if(temperatureController.PID.output <= -500){
        TIM1->CCR1 = 0;
        SetMotorSpeed(0,Abs(temperatureController.PID.output)+2000);
    }
    
    //分段式积分幅限
    if(temperatureController.setTemperature<=75)
        temperatureController.PID.integrationLimit=750;
    else if(temperatureController.setTemperature>75 && temperatureController.setTemperature<=125)
        temperatureController.PID.integrationLimit=50*temperatureController.setTemperature-3000;
    else if(temperatureController.setTemperature>125)
        temperatureController.PID.integrationLimit=3250;
    
    return 0;
}



